//
//  PickImageHandle.m
//  UILbrary
//
//  Created by Apple on 2023/3/1.
//  Copyright © 2023 mac. All rights reserved.
//

#import "PickImageHandle.h"

@import PhotosUI;



@interface PickImageHandle()<PHPickerViewControllerDelegate>

@property (nonatomic, strong) NSArray<NSString *> *identifiers;;

@end

@implementation PickImageHandle

- (void)updateImgIdList:(NSArray<NSString *> *)imgIdList
{
    if(!imgIdList) return;
    self.identifiers = imgIdList;
}

- (void)pickPhote
{
    PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
    PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:photoLibrary];
    configuration.filter = [PHPickerFilter imagesFilter];
    configuration.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeCurrent;
    if (@available(iOS 15, *)) {
        configuration.selection = PHPickerConfigurationSelectionOrdered;
        configuration.preselectedAssetIdentifiers = self.identifiers ?: @[];
    }
    configuration.selectionLimit = self.maxSelectImgCount;
    // 设置之前选择的 图片
//    configuration.preselectedAssetIdentifiers =
    PHPickerViewController *pickVc = [[PHPickerViewController alloc] initWithConfiguration:configuration];
    pickVc.delegate = self;
    [self.vc presentViewController:pickVc animated:YES
                     completion:nil];
}

//MARK: - PHPickerViewControllerDelegate

- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results
{
    [picker dismissViewControllerAnimated:YES completion:nil];

    self.identifiers = [results valueForKey:@"assetIdentifier"];
    
    if(!self.complete) return;
    self.complete();
}

//MARK: - get

- (NSArray<NSString *> *)imgIdList
{
    return self.identifiers;
}

@end
